<?php
namespace Nawa\Survey;
/**
 * Survery Questions.
 * 
 * A question is define with a language, a category,a type...
 * 
 * @author Xavier VILAIN
 * @package 
 * 
 * @property 	int 	$questionid	Question Id
 * @property 	int 	$surveryid	Survey Id
 * @property 	int 	$sectionid	Section Id 
 * @property	string	$language	Default Language
 * @property	int		$type		Question Type
 * @property	string	$content	Question Text
 * @property	int		$mandatory_answers_nb Mandatory Anwsers Number
 * 
 */
class Question extends \Fox\Object\Versioned {

	/**
	 * Link to answers Items
	 * @var string
	 */
	const LINK_ANSWERS = 'answers';

	/**
	 * Link to answers Items
	 * @var string
	 */
	const LINK_SECTION = 'section';
	
	/**
	 * Link to translation
	 * @var string
	 */
	const LINK_I18N = 'i18n';
	
	/**
	 * Reverse link to survey
	 */
	const LINK_SURVEY = 'survey';
	
	/**
	 * Process i18n to get the translated value if it exist,
	 * else the system will default to page value
	 * @param string $fieldName
	 * @return string
	 */
	public function i18nParse($fieldName) {
		$language = \Xview::instance()->language();
		$defaultLanguage = $this->language;
		if($language != $defaultLanguage) {
			$i18n = $this->findI18n($this->getInlineId().':'.$language);
			if($i18n->exists()) {
				return $i18n->$fieldName;
			}
			elseif(!empty($defaultLanguage)) {
				return $this->$fieldName;
			}
		}
		return $this->$fieldName;
	}
	
	/**
	 * Return Question Content
	 * @return string
	 */
	public function label() {
		return $this->i18nParse('content');
	}
	
	/**
	 * Return Helper
	 * @return string
	 */
	public function helper() {
		return $this->i18nParse('helper');
	}
		
	/**
	 * Render Question Answser
	 * @return string
	 */
	public function render($default = null,$readonly = false) {
		// Select Propers Anwser Renderer
		$output = $this->type()->renderer()->render($this,$default,$readonly);
		return $output;
	}
	
	/**
	 * Render Question Answser
	 * @return string
	 */
	public function renderAsPDF(\Fox\Renderer\Pdf &$pdf,$default = null,$readonly = false) {
		// Select Propers Anwser Renderer
		$output = $this->type()->renderAsPDF($pdf)->render($this,$default,$readonly);
		return $output;
	}
		
	/**
	 * Render Question Answser
	 * @return string
	 */
	public function type() {
		// Select Proper Anwser Renderer
		return \Fox\Dao::load(Question\Type::className(),$this->type);
	}
	
	/**
	 * 
	 * Render Question Tree Node
	 * @param \Fox\Node $questionNode
	 * @param string $indexPrefix
	 * @return string Html
	 */
	public static function renderTree(\Fox\Node $questionNode,$indexPrefix = null) {
		$question = $questionNode->getProperty('object');
		$helper = $question->helper();
		$default = $questionNode->getProperty('default');
		$readonly = $questionNode->getProperty('readonly');
		$output = '<li><span class="survey-question" >'.$indexPrefix.' '.$question->label().'</span>';
		$output .= !empty($helper) ? '<span class="survey-question-helper" >'.$helper.'</span>' : null;
		$output .= $question->render($default,$readonly);
		$output .= '</li>';
		if(!$questionNode->isEmpty()) {
			$output .= '<ul>';
					$index = 0;
			foreach($questionNode->getChildren() as $children) {
				$index++;
				$output .= self::renderTree($children,$indexPrefix.'.'.$index);
			}
			$output .= '</ul>';
		}
		return $output;
	}
	
	/**
	 * 
	 * Render Question Tree Node
	 * @param \Fox\Node $questionNode
	 * @param string $indexPrefix
	 * @return string Html
	 */
	public static function renderTreeAsPDF(\Fox\Renderer\Pdf &$pdf,\Fox\Node $questionNode,$indexPrefix = null) {
		$question = $questionNode->getProperty('object');
		$helper = $question->helper();
		$default = $questionNode->getProperty('default');
		// Question Label
		$qColumnWidth = 120;
		$pdf->Cell(10); 	// Right Margin
		$pdf->SetFont('','B',$pdf::FONT_SIZE_NORMAL);
		$pdf->MultiCell($qColumnWidth,7,$indexPrefix.' '.$question->label(),0,'L');
		
		if(!empty($helper)) {
			$pdf->Cell(15);
			$pdf->SetFont('','I',$pdf::FONT_SIZE_NORMAL);
			$pdf->MultiCell($qColumnWidth,5,$helper,0,'L');
			$pdf->Ln(2);
		}
		$pdf->SetFont('','',$pdf::FONT_SIZE_NORMAL);
		$output  = $question->renderAsPDF($pdf,$default);
		if(!$questionNode->isEmpty()) {
			$index = 0;
			foreach($questionNode->getChildren() as $children) {
				$index++;
				self::renderTreeAsPDF($pdf,$children,$indexPrefix.'.'.$index);
			}
		}
		return $output;
	}
}

\Fox\Dao::infos(Question::className(),
new \Fox\Object\Info('survey_section_questions', \Fox\Databases::DB_APP));

// Link to i18n
\Fox\Object\Links::add(\Fox\Dao::link(Question\I18n::className(),Question\I18n::LINK_QUESTION)
->to(Question::className(), Question::LINK_I18N)
->on('questionid')
->versionFrom());

// Link To External Source of Information
\Fox\Object\Links::add(\Fox\Dao::link(Question\Answer::className(),Question\Answer::LINK_QUESTION)
	->to(Question::className(), Question::LINK_ANSWERS)
	->on('questionid')
	->versionFrom()
	->orderBy('answerid')
);